package com.decent.ejfadmin.equity.cardproduct.dao;

import com.decent.ejfadmin.common.bean.Page;
import com.decent.ejfadmin.equity.cardproduct.dao.provider.CardProductDaoProvider;
import com.decent.ejfadmin.equity.common.CardOperator;
import com.decent.ejfadmin.equity.common.CardType;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 产品
 *
 * @author weicx
 * @date 2019/03/16
 */
@Mapper
@Repository
public interface CardProductDao {

    /**
     * 查询全部卡种
     *
     * @return
     */
    @Select("SELECT\n" +
            "id,\n" +
            "card_type_id,\n" +
            "card_operator_id,\n" +
            "card_operator_name,\n" +
            "`status`,\n" +
            "gmt_create_time,\n" +
            "gmt_modified_time,\n" +
            "remark\n" +
            "FROM\n" +
            "yuhuang_card_operator order by card_operator_id")
    List<CardOperator> queryAllCardOperatorList();

    /**
     * 查询权益卡种计数
     *
     * @param cardOperator 查询bean
     * @return list
     */
    @SelectProvider(type = CardProductDaoProvider.class, method = "queryCardOperatorCount")
    int queryCardOperatorCount(@Param("cardOperator") CardOperator cardOperator);

    /**
     * 查询权益卡种
     *
     * @param page
     * @param cardOperator
     * @return
     */
    @SelectProvider(type = CardProductDaoProvider.class, method = "queryCardOperatorList")
    List<CardOperator> queryCardOperatorList(@Param("page") Page page, @Param("cardOperator") CardOperator cardOperator);

    /**
     * 查询全部卡种
     *
     * @return
     */
    @Select("SELECT\n" +
            "id,\n" +
            "card_type_id,\n" +
            "card_type_name,\n" +
            "`status`,\n" +
            "gmt_create_time,\n" +
            "gmt_modified_time,\n" +
            "remark\n" +
            "FROM\n" +
            "yuhuang_card_type")
    List<CardType> queryAllCardTypeList();


    /**
     * 根据第三方id更新产品状态
     *
     * @param commodityId 商品id
     * @param status      状态
     * @return
     */
    @Update("update yuhuang_supply_product set state=#{status} where third_id=#{commodityId}")
    int updateEquityProductStatus(@Param("commodityId") Integer commodityId, @Param("status") int status);


    /**
     * 查询卡种编号是否存在
     *
     * @param cardOperatorId
     * @return
     */
    @Select("select * from yuhuang_card_operator where card_operator_id=#{cardOperatorId}")
    CardOperator queryCardOperator(@Param("cardOperatorId") Integer cardOperatorId);

    /**
     * 查询SPU是否存在
     *
     * @param ticketSpuid
     * @return
     */
    @Select("select count(1) from yuhuang_supply_product where ticket_spuid=#{ticketSpuid}")
    int querySpuByTicketProduct(@Param("ticketSpuid") Integer ticketSpuid);

    /**
     * 查询卡对应产品
     *
     * @param productType
     * @return
     */
    @Select("select count(1) from yuhuang_supply_product where product_type=#{productType} and submit_type=1")
    int queryProductByProductType(@Param("productType") Integer productType);

    /**
     * 查询卡券卡密spu是否重复
     *
     * @param spuId
     * @param productType
     * @return
     */
    @Select("select count(1) from yuhuang_supply_product where ticket_spuid=#{spuId} and product_type<>#{productType}")
    int querySpuTypeDuplicate(@Param("spuId") Integer spuId, @Param("productType") Integer productType);

    /**
     * 查询权益卡类型
     *
     * @return list
     */
    @Select("select id, card_type_name, status, remark, gmt_modified_time from yuhuang_card_type where id not in (9, 10, 11)")
    List<CardType> queryCardTypePage();

}
